package com.ruoyi.vote.app.controller;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.exception.vote.VoteException;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.framework.websocket.WebSocketUsers;
import com.ruoyi.vote.admin.domain.VoteActivity;
import com.ruoyi.vote.admin.domain.VoteSign;
import com.ruoyi.vote.admin.domain.bo.VoteLikeBo;
import com.ruoyi.vote.admin.domain.bo.VoteSignBo;
import com.ruoyi.vote.admin.domain.vo.VoteSignVo;
import com.ruoyi.vote.admin.service.IVoteActivityService;
import com.ruoyi.vote.admin.service.IVoteLikeService;
import com.ruoyi.vote.admin.service.IVoteSignService;
import com.ruoyi.vote.app.domain.request.SignJoinReq;
import com.ruoyi.vote.common.constants.ExamineConstants;
import com.ruoyi.vote.common.strategy.ActivityContext;
import com.ruoyi.vote.common.strategy.SignCheck;
import com.ruoyi.vote.common.strategy.UserLickCheck;
import com.sun.org.apache.bcel.internal.generic.NEW;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.websocket.Session;
import java.util.Arrays;
import java.util.Collections;

/**
 * API - 选手相关
 * @author 立勤
 * @date 2023/07/18 10:38:37
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/sign")
public class ApiSignController extends BaseController {

    private final IVoteSignService signService;
    private final IVoteLikeService likeService;
    private final IVoteActivityService activityService;

    /**
     * 参赛报名
     * @return
     */
    @PostMapping("/join")
    public R<Void> join(@Validated(AddGroup.class) @RequestBody SignJoinReq signJoinReq){
        VoteSignBo voteSign = BeanUtil.toBean(signJoinReq, VoteSignBo.class);
        return toAjax(signService.insertByBo(voteSign));
    }

    /**
     * 修改报名信息
     * @param signJoinReq
     * @return
     */
    @PostMapping("/update-sign-info")
    public R<Void> updateSignInfo(@Validated() SignJoinReq signJoinReq ){
        // 场景校验
        ActivityContext.setActivityStatus(new SignCheck());
        ActivityContext.checkActivity(String.valueOf(signJoinReq.getAId()));

        VoteSign voteSign = BeanUtil.toBean(signJoinReq, VoteSign.class);
        voteSign.setMId(LoginHelper.getUserId());
        return toAjax(signService.updateByBo(voteSign));
    }

    /**
     * 取消报名
     */
    @GetMapping("/{id}")
    public R<Void> cancelSign(@NotEmpty(message = "选手编号不能为空") @PathVariable Long id){
        VoteSignVo voteSignVo = signService.queryById(id);
        if (!voteSignVo.getStatus().equals(ExamineConstants.REVIEW)){
            // 状态异常
            throw new VoteException("vote.status.error");
        }
        VoteSign voteSign = new VoteSign();
        voteSign.setMId(LoginHelper.getUserId());
        voteSign.setId(id);
        voteSign.setStatus(ExamineConstants.CANCEL);
        return toAjax(signService.updateByBo(voteSign));
    }

    /**
     *  给选手投票
     * @param aid   活动ID
     * @param number   选手ID
     */
    @GetMapping("/vote/{aid}/{number}")
    public R<Void> vote(@PathVariable @NotNull(message = "活动不能为空") String aid, @PathVariable @NotNull(message = "选手编号不能为空") String number){
        Boolean aBoolean = likeService.likeUserVoting(String.valueOf(LoginHelper.getUserId()), aid, number);
        if (aBoolean){
            activityService.calculateRankingByaId(aid);
        }
        return toAjax(aBoolean);
    }

    /**
     * 预投票
     */
    @PostMapping("/pre-like")
    public R<Void> preLike(@Validated(AddGroup.class) @RequestBody VoteLikeBo bo) {
        bo.setMId(LoginHelper.getUserId());
        return toAjax(likeService.preLike(bo));
    }

}
